﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace y12fjydbs_1
{
    public partial class MySerialData
    {
        #region 执行控制器状态返回信息A2
        private void MsgYTA2()
        {
            string msg = "焰条 <-- 执行控制器状态返回信息:";
            string version = "";
            version = ((FindArr[7] & 0xf0) >> 4).ToString() + ".";
            version += ((FindArr[7] & 0x0f)).ToString();
            version += ((FindArr[8] & 0xf0) >> 4).ToString();
            if((FindArr[8] & 0x0f) != 0x00)
                version += ((char)((FindArr[8] & 0x0f) + 0x40)).ToString().ToLower();
            
            if (MyGlobal.frmYT != null)
            {
                msg += $"控制器状态{((FindArr[6] == 0x11) ? "准备好" : "未准备好")}  , 版本:{version}";
                MyGlobal.frmYT.RecviveMsg(msg, Color.Navy);
                MyGlobal.frmYT.RecviveMsgA2(FindArr[6] == 0x11);
            }
        }
        #endregion

        #region 执行控制器复位返回信息A7
        private void MsgYTA7()
        {
            string msg = "焰条 <-- 执行控制器复位返回信息:成功";

            if (MyGlobal.frmYT != null)
            {
                MyGlobal.frmYT.RecviveMsg(msg, Color.Navy);
                MyGlobal.frmYT.RecviveMsgA7();
            }

        }
        #endregion

        #region 焰条挂弹状态返回信息A1
        private void MsgYTA1()
        {
            string msg;
            if (FindArr[5] == 0xA1)
            {
                msg = "焰条 <-- 挂弹状态返回信息:";
            }
            else
            {
                msg = "焰条 <-- 查询挂弹测试结果返回信息:";
            }

            List<bool> list = new List<bool>();
            for (int i = 2; i >= 0; i--)
            {
                byte b = FindArr[i + 6];
                list.Add((b & 0x01) != 0);
                list.Add((b & 0x02) != 0);
                list.Add((b & 0x04) != 0);
                list.Add((b & 0x08) != 0);
                list.Add((b & 0x10) != 0);
                list.Add((b & 0x20) != 0);
                list.Add((b & 0x40) != 0);
                list.Add((b & 0x80) != 0);
            }
            YTType ytType;
            if (FindArr[9] == 0x01)
                ytType = YTType.冷云;
            else
                ytType = YTType.暖云;

            if (MyGlobal.frmYT != null)
            {
                MyGlobal.frmYT.RecviveMsg(msg, Color.Navy);
                MyGlobal.frmYT.RecviveMsgA1(ytType,list);
            }

        }
        #endregion

        #region 焰条播撒状态返回信息
        private void MsgYTA8()
        {
            string msg = "焰条 <-- 播撒状态返回信息:";

            if (FindArr[6] == 0xff)
            {
                msg += "已响应命令.";
                MyGlobal.frmYT.RecviveMsg(msg, Color.Navy);
            }
            else if (FindArr[6] == 0x0f)
            {
                msg += "播撒已完成";
                MyGlobal.frmYT.RecviveMsg(msg, Color.Navy);
            }
            else if (FindArr[6] == 0x07)
            {
                msg += "使能错信号错误,BS弹序号位无意义";
                MyGlobal.frmYT.RecviveMsg(msg, Color.Red);
            }
            List<bool> list = new List<bool>();

            for (int i = 2; i >= 0; i--)
            {
                byte b = FindArr[i + 7];
                list.Add((b & 0x01) != 0);
                list.Add((b & 0x02) != 0);
                list.Add((b & 0x04) != 0);
                list.Add((b & 0x08) != 0);
                list.Add((b & 0x10) != 0);
                list.Add((b & 0x20) != 0);
                list.Add((b & 0x40) != 0);
                list.Add((b & 0x80) != 0);
            }


            if (MyGlobal.frmYT != null)
            {
                MyGlobal.frmYT.RecviveMsgA8(FindArr[6], list);
            }

        }
        #endregion

        #region 焰条终止播撒返回信息
        private void MsgYTA5()
        {
            string msg = "焰条 --- 执行控制器中止焰弹播撒返回信息:成功";

            if (MyGlobal.frmYT != null)
            { 
                MyGlobal.frmYT.RecviveMsg(msg, Color.Navy);
                MyGlobal.frmYT.RecviveMsgA5();
            }

        }
        #endregion

        #region 烟弹播撒
        public void SendCmd_YTBS(byte yt_yd, byte left_right, byte timers, List<bool> list)
        {
            byte[] TmpCode = new byte[14];
            TmpCode[0] = 0xDB;
            TmpCode[1] = 0xCC;
            TmpCode[2] = yt_yd;
            TmpCode[3] = 14;
            TmpCode[4] = left_right; //0x01:左播撒器, 0x02:右播撒器
            TmpCode[5] = 0x48; // 播撒 指令识别码
            TmpCode[6] = timers;
             

            TmpCode[7] = 0x00;
            TmpCode[8] = 0x00;
            TmpCode[9] = 0x00;
            //for (int i = 19; i >= 16; i--)
            //{
            //    TmpCode[7] = (byte)(TmpCode[7] << 1);
            //    if (list[i])
            //    {
            //        TmpCode[7] = (byte)(TmpCode[7] | 0x01);
            //    }
            //}
            for (int i = 9; i >= 8; i--)
            {
                TmpCode[8] = (byte)(TmpCode[8] << 1);
                if (list[i])
                {
                    TmpCode[8] = (byte)(TmpCode[8] | 0x01);
                }
            }
            for (int i  = 7; i >= 0; i--)
            {
                TmpCode[9] = (byte)(TmpCode[9] << 1);
                if (list[i])
                {
                    TmpCode[9] = (byte)(TmpCode[9] | 0x01);
                }
            }

            TmpCode[10] = 0x00;
            TmpCode[11] = 0x00;
            TmpCode[12] = 0x00;
            TmpCode[13] = 0x00;
            for (int j = 0; j < 12; j++)
            {
                TmpCode[12] += TmpCode[j];
                TmpCode[13] += TmpCode[12];
            }
            Msg.WriteLine(cSendCount++.ToString(), 5);
            Msg.WriteLine($" -> {((yt_yd == 01) ? "焰条" : "焰弹")} 通道:{left_right.ToString()}   发送 [播撒] 命令", 5);
            cSerialPort.Send(TmpCode);
        }
        #endregion

    }
}
